package com.sunny.framework.system.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.framework.core.base.action.BaseAction;
import com.sunny.framework.system.entity.User;
import com.sunny.framework.system.service.UserService;

@Controller
@RequestMapping("system/user.html")
public class UserAction extends BaseAction<User> {

	private Logger logger = Logger.getLogger(UserAction.class);
	@Autowired
	private UserService service;

	@Override
	@RequestMapping(params = "list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("list");
		return "system/ListUser";
	}

	@Override
	public String add(HttpServletRequest reuqest, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@RequestMapping(params = "edit")
	public @ResponseBody
	Map<String, Object> edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.map_data(service.findById(Long.parseLong(request
					.getParameter("id"))));
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params = "delete")
	@Override
	public @ResponseBody
	Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.stringToLongArray(request
					.getParameter("id")));
			super.map_data();
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params = "listJson")
	public @ResponseBody
	Map<String, Object> listJson(Model model, HttpServletRequest request,
			HttpServletResponse response) {

		super.pager = pager(request);
		
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String sex = request.getParameter("sex");
		if (name != null && !name.isEmpty())
			criterions.add(Restrictions.eq("name", name));
		if (age != null && !age.isEmpty())
			criterions.add(Restrictions.eq("age", Integer.parseInt(age)));
		if (sex != null && !sex.isEmpty())
			criterions.add(Restrictions.eq("sex", sex));

		super.map_pager(service.find(criterions, pager.getFirstResults(),
				pager.getMaxResults()), service.num(criterions));
		return map;
	}

	@RequestMapping(params = "save")
	public @ResponseBody
	Map<String, Object> save(Model model, HttpServletRequest request,
			HttpServletResponse response, @ModelAttribute User user) {
		logger.info("save");
		try {
			service.save(user);
			super.map_data();
		} catch (Exception e) {
			e.printStackTrace();
			super.map_eror(e);
		}
		return map;
	}
}
